package fx.game.demo.event;

import fx.game.demo.common.LayoutManage;
import fx.game.demo.common.WindowManage;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

/**
 * 获取按钮事件
 */
public class ButtonEvent {

    /**
     * 重置游戏事件
     */
    private static EventHandler<ActionEvent> resetEvent;

    /**
     * 打开颜色表窗口事件
     */
    private static EventHandler<ActionEvent> openColorTableWindowEvent;

    /**
     * 游戏失败弹窗确定按钮事件
     */
    private static EventHandler<ActionEvent> gameFailConfirmEvent;

    /**
     * 游戏结束窗口关闭事件
     */
    private static ChangeListener<Boolean> gameOverWindowOnCloseEvent;

    /**
     * 打开关于页面事件
     */
    private static EventHandler<ActionEvent> openAboutWindow;

    /**
     * @Author: datom
     * @Date: 2021/12/6 18:10
     *
     * @Description: 重置游戏事件
     *
     */
    public static EventHandler<ActionEvent> getResetEvent() {
        if(resetEvent == null) {
            resetEvent = new EventHandler<ActionEvent>(){

                @Override
                public void handle(ActionEvent event) {
                    LayoutManage.getGameLayout().reset();
                }
            };
        }
        return resetEvent;
    }

    /**
     * @Author: datom
     * @Date: 2021/12/6 18:13
     *
     * @Description: 打开颜色表窗口事件
     *
     */
    public static EventHandler<ActionEvent> getOpenColorTableWindowEvent() {
        if(openColorTableWindowEvent == null) {
            openColorTableWindowEvent = new EventHandler<ActionEvent>(){

                @Override
                public void handle(ActionEvent event) {
                    if(! WindowManage.getColorTableWindow().isShowing()) {
                        WindowManage.getColorTableWindow().show();
                    }
                }
            };
        }
        return openColorTableWindowEvent;
    }

    /**
     * @Author: datom
     * @Date: 2021/12/6 18:20
     *
     * @Description: 游戏失败弹窗确定按钮事件
     *
     */
    public static EventHandler<ActionEvent> getGameFailConfirmEvent() {
        if(gameFailConfirmEvent == null) {
            gameFailConfirmEvent = new EventHandler<ActionEvent>(){

                @Override
                public void handle(ActionEvent event) {
                    if(WindowManage.getGameOverTips().isShowing()) {
                        WindowManage.getGameOverTips().close();
                    }
                    //重置游戏
                    LayoutManage.getGameLayout().reset();
                }
            };
        }
        return gameFailConfirmEvent;
    }

    /**
     * @Author: datom
     * @Date: 2021/12/6 19:27
     *
     * @Description: 游戏结束窗口关闭事件
     *
     */
    public static ChangeListener<Boolean> getGameOverWindowOnCloseEvent() {
        if(gameOverWindowOnCloseEvent == null) {
            gameOverWindowOnCloseEvent = new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    if(!newValue) {
                        getGameFailConfirmEvent().handle(null);
                    }
                }};
        }
        return gameOverWindowOnCloseEvent;
    }

    /**
     * @Author: datom
     * @Date: 2021/12/7 0:06
     *
     * @Description: 获取打开关于页面的事件
     *
     */
    public static EventHandler<ActionEvent> getOpenAboutWindowEvent() {
        if(openAboutWindow == null) {
            openAboutWindow = new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    if(!WindowManage.getAboutWindow().isShowing()) {
                        WindowManage.getAboutWindow().show();
                    }
                }
            };
        }
        return openAboutWindow;
    }

}
